import {text} from './Text';
import store from 'store';

const wordWeight = store.namespace('weight');
const textStorage = store.namespace('text');

let memory_index = textStorage.get('memory_index') || 0;
text.push(...textStorage.get('customText') || []);
export function getWordList() {
    return text;
}

export function currentWord() {
    return text[memory_index];
}

export function setNextWord() {
    if (memory_index >= text.length) return;
    textStorage.set('memory_index', ++memory_index);
    return text[memory_index];
}

export function setPrevWord() {
    if (memory_index <= 0) return;
    textStorage.set('memory_index', --memory_index);
    return text[memory_index];
}

export function getMemoryIndex() {
    return memory_index;
}


export function getWeight(word) {
    return Number(wordWeight.get(word) || 0);
}

export function addWords(words) {
    const list = textStorage.get('customText') || [];
    words = words.split('');
    list.push(...words);
    text.push(...words);
    textStorage.set('customText', ...words);
}

export function setWeightIncrease(word, weight) {
    // 永久记忆方法：重复29遍。
    weight = Math.min(weight + 1, 29);
    wordWeight.set(word, weight);
    return weight;
}

export function setWeightReduction(word, weight) {
    weight = Math.max(weight - 1, 0);
    wordWeight.set(word, weight);
    return weight;
}